﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace per.esmool.utils {

    public class FormatKit {

        private static readonly Regex NUMBER = new(@"\d+");
        private static readonly Regex ASSET = new(@"s:.+?");

        public Func<string, string> RandomTextInterpoletor { get; set; }
        public Func<string, string> AssetInterpoler { get; set; }

        public string Interpolate(string text, params string[] args) {
            var tokens = Tokenize(text);
            var builder = new StringBuilder();
            List<string> parameters = [..args];
            foreach (var token in tokens) {
                if (token[0] != '{' || token[1] == '{') {
                    builder.Append(token.Replace("}", "}}"));
                    continue;
                }

                if (token[^1] != '}') {
                    builder.Append(token.Replace("{", "{{"));
                    continue;
                }

                if (token[0] != '{') {
                    builder.Append(token.Replace("}", "}}"));
                    continue;
                }

                var expression = token[^1] == '}' ? token[1..^1] : token[1..];

                if (NUMBER.IsMatch(expression)) {
                    builder.Append(token);
                    continue;
                }

                var value = ASSET.IsMatch(expression) ?
                    this.AssetInterpoler.Invoke(expression) :
                    this.RandomTextInterpoletor.Invoke(expression);
                parameters.Add(value);

                var placeholder = $"{{{parameters.Count - 1}}}";
                builder.Append(placeholder);
            }
            var format = builder.ToString();
            var result = string.Format(format, [..parameters]);
            return result;
        }

        public static List<string> Tokenize(string text) {
            var ptr = 0;
            var tokens = new List<string>();
            var cache = new Queue<char>();
            var foundBra = false;

            void Collect() {
                var text = new string(cache.ToArray());
                cache.Clear();
                if (string.IsNullOrEmpty(text)) return;
                tokens.Add(text);
            }

            while (ptr < text.Length) {
                var c = text[ptr++];
                var d = ptr < text.Length ? text[ptr] : '\0';

                if (!foundBra) {
                    if (c == '{') {
                        if (d == '{') {
                            cache.Enqueue(c);
                            cache.Enqueue(d);
                            ptr++;
                            continue;
                        }

                        Collect();
                        foundBra = true;
                    }

                    cache.Enqueue(c);
                    continue;
                }

                cache.Enqueue(c);

                if (c == '}') {
                    Collect();
                    foundBra = false;
                }
            }

            Collect();
            return tokens;
        }
    }

}
